import { searchBookByISBN, uploadBookCover } from "../Api/index";
const state = {
  curInputBook: null,
};
const mutations = {
  SET_CURRENT_BOOK(state, book) {
    state.curInputBook = book;
  },
};
const actions = {
  // 上传书籍封面成功返回地址，失败返回一个错误信息
  async uploadBookCover({ commit }, file) {
    const result = await uploadBookCover(file);
    const data = JSON.parse(result.data);
    if (data.status == 200) {
        return data.data; //返回地址，data.data中存储着图片地址
    } else new Promise.reject(new Error("查询失败"));
  },

  // 通过ISBN搜索书籍信息
  async searchBookByISBN({ commit }, ISBN) {
    let reslut = await searchBookByISBN(ISBN);
    if (reslut.data.status === 200) {
      commit("SET_CURRENT_BOOK", reslut.data.data);
      return "ok";
    } else {
      new Promise.reject(new Error("查询失败"));
    }
  },
};
const getters = {
  curInputBook(state) {
    return state.curInputBook;
  },
};
export default {
  state,
  mutations,
  actions,
  getters,
};
